Separating Control Layer
#udemy
#Unity
現在、Moverクラスに入力ロジックが実装されているが、
Player Controlerに分ける
Separate Logic
https://gyazo.com/3f84fbc6683d8412f8bc0d54bb369506
キャラクターの操作と、Player, Enemyの入力を分離すると、
同じような動作を敵味方で使い回せる。
(Multiplayerでも有効)
Player Controller
Inputの受け取り、RayCastの照射をこちらに移す
MoverにMoveToメソッドを作って呼び出す
code: Player.fs
namespace FSharp
open UnityEngine
type PlayerController() =
inherit MonoBehaviour()
<DefaultValue>
val mutable private lastLay: Ray
<DefaultValue>
val mutable hit: RaycastHit
member private this.Update() =
if Input.GetMouseButton(0) then
do this.lastLay <- Camera.main.ScreenPointToRay(Input.mousePosition)
this.MoveToCursor()
Debug.DrawRay(this.lastLay.origin, (float32 (100) * this.lastLay.direction))
member private this.MoveToCursor() =
Camera.main.ScreenPointToRay(Input.mousePosition)
|> (fun ray -> Physics.Raycast(ray, &this.hit))
|> (fun isHit ->
if isHit
then this.GetComponent<Mover>().MoveTo(this.hit.point)
else ())
VSCode Ionide-fsharpなら、
プロジェクト(ソリューションの下)を右クリックして、Add Fileするとかんたんに依存を解決しつつ追加できる
Create Prefab
https://gyazo.com/e780c5fef4b64e4596d4cd1c2c47cf16
Enemy, Player の Prefabを作っておく
コレをクリックすればそのPrefab edit scene を開ける